<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="{{url_for('static', filename='jsencrypt.min.js')}}"></script>
    </head>

    <body>

        <form action="/send" id="form" method="post">
            <label>Имя компьютера:</label>
            <input name="hostname"/>

            <label>Пароль:</label>
            <input id="ssh-pass" name="ssh-password" type="password"/>

            <label>Уведомление</label>
            <input />

            <button type="submit">Отправить</button>
        </form>

        <input type="checkbox"/>
        <textarea hidden id="publickey"></textarea>
    </body>
    
</html>

<script>
    var cryptor = new JSEncrypt();

    function getPkey(){
        var xhttp = new XMLHttpRequest();
        var publicKey = ''
        xhttp.open("GET", "/publickey", false);
        xhttp.setRequestHeader("Content-Type", "application/json");
        xhttp.onreadystatechange = function(){
            if (this.readyState == 4 && this.status == 200){
                publicKey = this.responseText
            }
        }
        xhttp.send();

        return publicKey;
    }    

    document.addEventListener("DOMContentLoaded", function(){
        var pkey = getPkey();
        cryptor.setPublicKey(pkey)
        document.getElementById('publickey').value = pkey
    })

    document.getElementById("form").onsubmit = function() {
        var encrypt = new JSEncrypt();
        var passwordField = document.getElementById('ssh-pass')
        passwordField.value = cryptor.encrypt(passwordField.value)
    }
</script>